package com.huizhonglm.diap.presenters.app;

import com.alibaba.fastjson.JSON;
import com.bruce.common.utils.EmptyUtil;
import com.huizhonglm.diap.api.API;
import com.huizhonglm.diap.api.HttpCallback;
import com.huizhonglm.diap.api.model.app.CheckUpdateParams;
import com.huizhonglm.diap.api.model.app.CheckUpdateResponse;
import com.huizhonglm.diap.presenters.BasePresenter;
import com.huizhonglm.diap.ui.app.iView.CheckUpdateIView;

/**
 * 描述当前类的作用
 *
 * @author LiXiaoLong
 * @version 1.0
 * @since 2016-03-01 17:37
 */
public class CheckUpdatePersenter extends BasePresenter {

    private CheckUpdateIView iView;

    public CheckUpdatePersenter(CheckUpdateIView iView) {
        this.iView = iView;
    }

    public void checkUpdate(){
        API.http().post("checkUpdate", new CheckUpdateParams(), new HttpCallback() {
            @Override
            public void onSuccess(String result) {
                if (!EmptyUtil.isEmpty(result)) {
                    CheckUpdateResponse resp = JSON.parseObject(result, CheckUpdateResponse.class);
                    if (resp != null) {
                        iView.onCheckVersionSuccess(resp);
                    } else {
                        iView.onCheckVersionSuccess(null);
                    }
                } else {
                    iView.onCheckVersionSuccess(null);
                }
            }

            @Override
            public void onFailure(Throwable ex) {
                iView.onCheckVersionFailure();
            }
        });
    }
}
